home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / dispatch.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  9KB  |  258 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import types
  5. import os
  6. import sys
  7. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  8. from ZSI import *
  9. from ZSI import _child_elements, _copyright, _seqtypes, _find_arraytype, _find_type, resolvers
  10. from ZSI.auth import _auth_tc, AUTH, ClientBinding
  11. _client_binding = None
  12.  
  13. def GetClientBinding():
  14.     return _client_binding
  15.  
  16.  
  17. gettypecode = lambda mod, e: getattr(mod, str(e.localName)).typecode
  18.  
  19. def _Dispatch(ps, modules, SendResponse, SendFault, nsdict = { }, typesmodule = None, gettypecode = gettypecode, rpc = False, docstyle = False, **kw):
  20.     global _client_binding
  21.     
  22.     try:
  23.         what = str(ps.body_root.localName)
  24.         if modules is None:
  25.             modules = (sys.modules['__main__'],)
  26.         
  27.         handlers = _[1]
  28.         handlers = _[2]
  29.         if len(handlers) > 1:
  30.             raise TypeError('Multiple implementations found: ' + `handlers`)
  31.         
  32.         handler = handlers[0]
  33.         _client_binding = ClientBinding(ps)
  34.         None if docstyle else None<EXCEPTION MATCH>AttributeError
  35.         if typesmodule is not None:
  36.             kwargs = { }
  37.             for e in _child_elements(ps.body_root):
  38.                 
  39.                 try:
  40.                     tc = gettypecode(typesmodule, e)
  41.                 except Exception:
  42.                     tc = TC.Any()
  43.  
  44.                 
  45.                 try:
  46.                     kwargs[str(e.localName)] = tc.parse(e, ps)
  47.                 continue
  48.                 except EvaluateException:
  49.                     ex = None
  50.                     SendFault(FaultFromZSIException(ex), **kw)
  51.                     return None
  52.                     continue
  53.                 
  54.  
  55.             
  56.             result = handler(**kwargs)
  57.             aslist = False
  58.             if type(result) in _seqtypes:
  59.                 for o in result:
  60.                     aslist = hasattr(result, 'typecode')
  61.                     if aslist:
  62.                         break
  63.                         continue
  64.                     None<EXCEPTION MATCH>EvaluateException
  65.                 
  66.             elif type(result) is not dict:
  67.                 aslist = not hasattr(result, 'typecode')
  68.                 result = (result,)
  69.             
  70.             tc = TC.Any(pname = what + 'Response', aslist = aslist)
  71.         else:
  72.             tp = _find_type(ps.body_root)
  73.             if not type(tp) in (tuple, list) or tp[1] == 'Array':
  74.                 pass
  75.             isarray = _find_arraytype(ps.body_root)
  76.             data = _child_elements(ps.body_root)
  77.             tc = TC.Any()
  78.             if isarray and len(data) == 0:
  79.                 result = handler()
  80.             elif isarray:
  81.                 
  82.                 try:
  83.                     arg = [ tc.parse(e, ps) for e in data ]
  84.                 except EvaluateException:
  85.                     e = None
  86.                     SendFault(RuntimeError('THIS IS AN ARRAY: %s' % isarray))
  87.                     return None
  88.  
  89.                 result = handler(*arg)
  90.             else:
  91.                 
  92.                 try:
  93.                     kwarg = []([ (str(e.localName), tc.parse(e, ps)) for e in data ])
  94.                 except EvaluateException:
  95.                     e = None
  96.                     SendFault(FaultFromZSIException(e), **kw)
  97.                     return None
  98.  
  99.                 result = handler(**kwarg)
  100.             tc = TC.Any(pname = what + 'Response')
  101.         sw = SoapWriter(nsdict = nsdict)
  102.         sw.serialize(result, tc)
  103.         return SendResponse(str(sw), **kw)
  104.     except Fault:
  105.         e = None
  106.         return SendFault(e, **kw)
  107.     except Exception:
  108.         e = None
  109.         return SendFault(FaultFromException(e, 0, sys.exc_info()[2]), **kw)
  110.  
  111.  
  112.  
  113. def _ModPythonSendXML(text, code = 200, **kw):
  114.     req = kw['request']
  115.     req.content_type = 'text/xml'
  116.     req.content_length = len(text)
  117.     req.send_http_header()
  118.     req.write(text)
  119.  
  120.  
  121. def _ModPythonSendFault(f, **kw):
  122.     _ModPythonSendXML(f.AsSOAP(), 500, **kw)
  123.  
  124.  
  125. def _JonPySendFault(f, **kw):
  126.     _JonPySendXML(f.AsSOAP(), 500, **kw)
  127.  
  128.  
  129. def _JonPySendXML(text, code = 200, **kw):
  130.     req = kw['request']
  131.     req.set_header('Content-Type', 'text/xml; charset="%s"' % UNICODE_ENCODING)
  132.     req.set_header('Content-Length', str(len(text)))
  133.     req.write(text)
  134.  
  135.  
  136. def _CGISendXML(text, code = 200, **kw):
  137.     print 'Status: %d' % code
  138.     print 'Content-Type: text/xml; charset="%s"' % UNICODE_ENCODING
  139.     print 'Content-Length: %d' % len(text)
  140.     print ''
  141.     print text
  142.  
  143.  
  144. def _CGISendFault(f, **kw):
  145.     _CGISendXML(f.AsSOAP(), 500, **kw)
  146.  
  147.  
  148. class SOAPRequestHandler(BaseHTTPRequestHandler):
  149.     server_version = 'ZSI/1.1 ' + BaseHTTPRequestHandler.server_version
  150.     
  151.     def send_xml(self, text, code = 200):
  152.         self.send_response(code)
  153.         if text:
  154.             self.send_header('Content-type', 'text/xml; charset="%s"' % UNICODE_ENCODING)
  155.             self.send_header('Content-Length', str(len(text)))
  156.         
  157.         self.end_headers()
  158.         if text:
  159.             self.wfile.write(text)
  160.         
  161.         self.wfile.flush()
  162.  
  163.     
  164.     def send_fault(self, f, code = 500):
  165.         self.send_xml(f.AsSOAP(), code)
  166.  
  167.     
  168.     def do_POST(self):
  169.         
  170.         try:
  171.             ct = self.headers['content-type']
  172.             if ct.startswith('multipart/'):
  173.                 cid = resolvers.MIMEResolver(ct, self.rfile)
  174.                 xml = cid.GetSOAPPart()
  175.                 ps = ParsedSoap(xml, resolver = cid.Resolve)
  176.             else:
  177.                 length = int(self.headers['content-length'])
  178.                 ps = ParsedSoap(self.rfile.read(length))
  179.         except ParseException:
  180.             e = None
  181.             self.send_fault(FaultFromZSIException(e))
  182.             return None
  183.         except Exception:
  184.             e = None
  185.             self.send_fault(FaultFromException(e, 1, sys.exc_info()[2]))
  186.             return None
  187.  
  188.         _Dispatch(ps, self.server.modules, self.send_xml, self.send_fault, docstyle = self.server.docstyle, nsdict = self.server.nsdict, typesmodule = self.server.typesmodule, rpc = self.server.rpc)
  189.  
  190.  
  191.  
  192. def AsServer(port = 80, modules = None, docstyle = False, nsdict = { }, typesmodule = None, rpc = False, addr = ''):
  193.     address = (addr, port)
  194.     httpd = HTTPServer(address, SOAPRequestHandler)
  195.     httpd.modules = modules
  196.     httpd.docstyle = docstyle
  197.     httpd.nsdict = nsdict
  198.     httpd.typesmodule = typesmodule
  199.     httpd.rpc = rpc
  200.     httpd.serve_forever()
  201.  
  202.  
  203. def AsCGI(nsdict = { }, typesmodule = None, rpc = False, modules = None):
  204.     if os.environ.get('REQUEST_METHOD') != 'POST':
  205.         _CGISendFault(Fault(Fault.Client, 'Must use POST'))
  206.         return None
  207.     
  208.     ct = os.environ['CONTENT_TYPE']
  209.     
  210.     try:
  211.         if ct.startswith('multipart/'):
  212.             cid = resolvers.MIMEResolver(ct, sys.stdin)
  213.             xml = cid.GetSOAPPart()
  214.             ps = ParsedSoap(xml, resolver = cid.Resolve)
  215.         else:
  216.             length = int(os.environ['CONTENT_LENGTH'])
  217.             ps = ParsedSoap(sys.stdin.read(length))
  218.     except ParseException:
  219.         e = None
  220.         _CGISendFault(FaultFromZSIException(e))
  221.         return None
  222.  
  223.     _Dispatch(ps, modules, _CGISendXML, _CGISendFault, nsdict = nsdict, typesmodule = typesmodule, rpc = rpc)
  224.  
  225.  
  226. def AsHandler(request = None, modules = None, **kw):
  227.     ps = ParsedSoap(request)
  228.     kw['request'] = request
  229.     _Dispatch(ps, modules, _ModPythonSendXML, _ModPythonSendFault, **kw)
  230.  
  231.  
  232. def AsJonPy(request = None, modules = None, **kw):
  233.     kw['request'] = request
  234.     if request.environ.get('REQUEST_METHOD') != 'POST':
  235.         _JonPySendFault(Fault(Fault.Client, 'Must use POST'), **kw)
  236.         return None
  237.     
  238.     ct = request.environ['CONTENT_TYPE']
  239.     
  240.     try:
  241.         if ct.startswith('multipart/'):
  242.             cid = resolvers.MIMEResolver(ct, request.stdin)
  243.             xml = cid.GetSOAPPart()
  244.             ps = ParsedSoap(xml, resolver = cid.Resolve)
  245.         else:
  246.             length = int(request.environ['CONTENT_LENGTH'])
  247.             ps = ParsedSoap(request.stdin.read(length))
  248.     except ParseException:
  249.         e = None
  250.         _JonPySendFault(FaultFromZSIException(e), **kw)
  251.         return None
  252.  
  253.     _Dispatch(ps, modules, _JonPySendXML, _JonPySendFault, **kw)
  254.  
  255. if __name__ == '__main__':
  256.     print _copyright
  257.  
  258.